#!/bin/bash

CLASSPATH=`pwd`/engine/clojure.jar:`pwd`/engine/clojure-contrib.jar:`pwd`/engine/rete.jar

FACTS_PATH=`pwd`/data/facts/
RESULTS=`pwd`/data/results.xml
RULES=`pwd`/data/rules/rules.xml
DB=`pwd`/data/bolsar.db


echo "[*] Deleting old facts and results"
rm $FACTS_PATH/*
rm $RESULTS
echo "[*] Done."
echo


#ONLY FOR DEBUG
cp data/test_fact.xml $FACTS_PATH

echo "[*] Scrapping bolsar.com.ar..."
python scraper/scrap_bolsar.py $DB 2>/dev/null >/dev/null
echo "[*] Done."

echo

echo "[*] Generating facts from database..."
python scraper/facts_generator.py $FACTS_PATH $DB 2>/dev/null >/dev/null
echo "[*] Done."

echo

echo "[*] Running engine"


cd engine

echo "<results>" >> $RESULTS

FILES=$FACTS_PATH*
for f in $FILES
do
  filename=$(basename "$f")
  extension="${filename##*.}"
  filename="${filename%.*}"



  echo "<result symbol='$filename'>" >> $RESULTS
  echo "   Processing $filename"
    java -cp clojure.jar -classpath $CLASSPATH RunEngine $RULES $f >> $RESULTS
  # take action on each file. $f store current file name
  echo "</result>" >> $RESULTS
done

echo "</results>" >> $RESULTS

echo "[*] Done."


